﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableUtility
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

internal class CApCableUtility
{
  public static string NumericLinearFormat
  {
    get => "F" + TxApplication.Options.Units.LinearDigitsAfterDecimalPoint.ToString();
  }

  public static string NumericAngularFormat
  {
    get => "F" + TxApplication.Options.Units.AngularDigitsAfterDecimalPoint.ToString();
  }

  public static string LinearUnitLabelText() => $" ({TxApplication.Options.Units.LinearUnitName}):";

  public static string LinearUnitHeaderText() => $" ({TxApplication.Options.Units.LinearUnitName})";

  public static string MassUnitLabelText() => $" ({TxApplication.Options.Units.MassUnitName}):";

  public static double ComputeLinearRoundValueWithCurrentUnit(double value)
  {
    double num = Math.Pow(10.0, (double) (-1 * TxApplication.Options.Units.LinearDigitsAfterDecimalPoint));
    return value / TxApplication.Options.Units.LinearMultiplier + num * 0.99;
  }

  public static void OrderedAttachmentList(
    List<CApFLEXAttachment> attachments,
    out List<CApFLEXAttachment> orderedAttachments)
  {
    orderedAttachments = (List<CApFLEXAttachment>) null;
    if (attachments.Count <= 0)
      return;
    orderedAttachments = new List<CApFLEXAttachment>();
    orderedAttachments.Add(attachments[0]);
    for (int index1 = 1; index1 < attachments.Count; ++index1)
    {
      bool flag = false;
      for (int index2 = 0; index2 < orderedAttachments.Count; ++index2)
      {
        if (attachments[index1] < orderedAttachments[index2])
        {
          flag = true;
          orderedAttachments.Insert(index2, attachments[index1]);
          break;
        }
      }
      if (!flag)
        orderedAttachments.Add(attachments[index1]);
    }
    int num = 1;
    foreach (CApFLEXAttachment capFlexAttachment in orderedAttachments)
    {
      capFlexAttachment.IndexFixedFrame = num;
      ++num;
    }
  }

  public static bool IsInRobotHierarchy(ITxObject obj, ITxRobot robot)
  {
    bool flag = false;
    if (obj != null && robot != null)
    {
      if (obj == robot)
        flag = true;
      if (!flag && TxEngineeringDataInternal.GetScope(obj) is TxRobot && TxEngineeringDataInternal.GetScope(obj) == robot)
        flag = true;
    }
    return flag;
  }

  public static bool IsAttached(ITxLocatableObject obj, ITxObjectCollection objParent)
  {
    bool flag = false;
    if (objParent != null && obj != null)
    {
      if (obj == objParent)
      {
        flag = true;
      }
      else
      {
        obj = CApCableUtility.GetClosestAttachedPreservedCollection((ITxObject) obj) as ITxLocatableObject;
        if (obj == objParent)
        {
          flag = true;
        }
        else
        {
          TxTypeFilter attachementFrameFilter = CApCableUtility.GetAttachementFrameFilter();
          if (CApCableUtility.IsAttachmentDescendants(obj, objParent as ITxLocatableObject, attachementFrameFilter))
          {
            flag = true;
          }
          else
          {
            foreach (ITxObject directDescendant in (Collection<ITxObject>) objParent.GetDirectDescendants((ITxTypeFilter) attachementFrameFilter))
            {
              if (directDescendant == obj)
              {
                flag = true;
                break;
              }
              if (!CApCableUtility.IsAttachmentDescendants(obj, directDescendant as ITxLocatableObject, attachementFrameFilter))
              {
                if (CApCableUtility.IsAttached(obj, directDescendant as ITxObjectCollection))
                {
                  flag = true;
                  break;
                }
              }
              else
              {
                flag = true;
                break;
              }
            }
          }
        }
      }
    }
    return flag;
  }

  public static bool IsAttachmentDescendants(
    ITxLocatableObject obj,
    ITxLocatableObject parent,
    TxTypeFilter filter)
  {
    bool flag = false;
    TxObjectList attachmentDescendants = parent.GetDirectAttachmentDescendants((ITxTypeFilter) filter);
    foreach (ITxObject itxObject in (Collection<ITxObject>) attachmentDescendants)
    {
      if (obj == itxObject)
      {
        flag = true;
        break;
      }
    }
    if (!flag)
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) attachmentDescendants)
      {
        if (itxObject is ITxObjectCollection objectCollection)
        {
          foreach (ITxObject allDescendant in (Collection<ITxObject>) objectCollection.GetAllDescendants((ITxTypeFilter) filter))
          {
            if (allDescendant == obj)
            {
              flag = true;
              break;
            }
          }
        }
      }
    }
    if (!flag)
    {
      foreach (ITxObject parent1 in (Collection<ITxObject>) attachmentDescendants)
      {
        if (CApCableUtility.IsAttachmentDescendants(obj, parent1 as ITxLocatableObject, filter))
        {
          flag = true;
          break;
        }
      }
    }
    return flag;
  }

  public static bool IsAttachmentDescendants(
    ITxObject obj,
    ITxLocatableObject parent,
    TxTypeFilter filter)
  {
    bool flag = false;
    TxObjectList attachmentDescendants = parent.GetDirectAttachmentDescendants((ITxTypeFilter) filter);
    foreach (ITxObject itxObject in (Collection<ITxObject>) attachmentDescendants)
    {
      if (obj == itxObject)
      {
        flag = true;
        break;
      }
    }
    if (!flag)
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) attachmentDescendants)
      {
        if (itxObject is ITxObjectCollection objectCollection)
        {
          foreach (ITxObject allDescendant in (Collection<ITxObject>) objectCollection.GetAllDescendants((ITxTypeFilter) filter))
          {
            if (allDescendant == obj)
            {
              flag = true;
              break;
            }
          }
        }
      }
    }
    if (!flag)
    {
      foreach (ITxObject parent1 in (Collection<ITxObject>) attachmentDescendants)
      {
        if (CApCableUtility.IsAttachmentDescendants(obj, parent1 as ITxLocatableObject, filter))
        {
          flag = true;
          break;
        }
      }
    }
    return flag;
  }

  public static void ComputeRobotFrameAttachement(
    ITxLocatableObject frame,
    out ITxObject robotFrame)
  {
    robotFrame = (ITxObject) null;
    if (frame == null)
      return;
    TxObjectList allDescendants = TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxRobot)));
    foreach (ITxObject objParent in (Collection<ITxObject>) allDescendants)
    {
      if (CApCableUtility.IsAttached(frame, objParent as ITxObjectCollection))
      {
        robotFrame = objParent;
        break;
      }
    }
    if (robotFrame != null)
      return;
    ITxGun objParent1 = (ITxGun) null;
    foreach (ITxRobot itxRobot in (Collection<ITxObject>) allDescendants)
    {
      for (int index = 0; index < ((Collection<ITxObject>) itxRobot.MountedTools).Count; ++index)
      {
        if (((Collection<ITxObject>) itxRobot.MountedTools)[index] is ITxGun)
        {
          objParent1 = ((Collection<ITxObject>) itxRobot.MountedTools)[index] as ITxGun;
          break;
        }
      }
      if (objParent1 != null && CApCableUtility.IsAttached(frame, objParent1 as ITxObjectCollection))
      {
        robotFrame = (ITxObject) itxRobot;
        break;
      }
    }
  }

  public static void ComputeKinematicLinkFrameAttachment(
    ITxLocatableObject frame,
    out TxKinematicLink attachedKinematicLink)
  {
    attachedKinematicLink = (TxKinematicLink) null;
    if (frame == null)
      return;
    ITxLocatableObject preservedCollection = CApCableUtility.GetClosestAttachedPreservedCollection((ITxObject) frame) as ITxLocatableObject;
    while (true)
    {
      switch (preservedCollection)
      {
        case null:
        case TxKinematicLink _:
          goto label_4;
        default:
          preservedCollection = CApCableUtility.GetClosestAttachedPreservedCollection((ITxObject) preservedCollection.AttachmentParent) as ITxLocatableObject;
          continue;
      }
    }
label_4:
    if (!(preservedCollection is TxKinematicLink))
      return;
    attachedKinematicLink = preservedCollection as TxKinematicLink;
  }

  private static TxTypeFilter GetAttachementFrameFilter()
  {
    TxTypeFilter attachementFrameFilter = new TxTypeFilter(typeof (TxComponent));
    attachementFrameFilter.AddIncludedType(typeof (TxLogicalGroup));
    attachementFrameFilter.AddIncludedType(typeof (TxGun));
    attachementFrameFilter.AddIncludedType(typeof (TxRobot));
    attachementFrameFilter.AddIncludedType(typeof (TxTcTypeDefinition));
    attachementFrameFilter.AddIncludedType(typeof (TxKinematicLink));
    attachementFrameFilter.AddIncludedType(typeof (TxServoGun));
    attachementFrameFilter.AddIncludedType(typeof (ITxContinuousOperation));
    attachementFrameFilter.AddIncludedType(typeof (TxWeldPoint));
    attachementFrameFilter.AddIncludedType(typeof (TxTcTypeDefinition));
    attachementFrameFilter.AddIncludedType(typeof (TxCompoundResource));
    attachementFrameFilter.AddIncludedType(typeof (TxCompoundPartEx));
    attachementFrameFilter.AddIncludedType(typeof (TxPartInstanceEx));
    attachementFrameFilter.AddIncludedType(typeof (TxPartEx));
    attachementFrameFilter.AddIncludedType(typeof (TxFrame));
    attachementFrameFilter.AddIncludedType(typeof (TxGripper));
    attachementFrameFilter.AddIncludedType(typeof (TxDevice));
    attachementFrameFilter.AddExcludedType(typeof (TxMfgFeature));
    attachementFrameFilter.AddExcludedType(typeof (TxWeldPoint));
    attachementFrameFilter.AddExcludedType(typeof (TxContinuousRoboticOperation));
    return attachementFrameFilter;
  }

  public static void SelectObject(ITxObject txObject)
  {
    if (txObject == null)
      return;
    TxObjectList txObjectList1 = new TxObjectList(1);
    ((Collection<ITxObject>) txObjectList1).Add(txObject);
    TxObjectList txObjectList2 = txObjectList1;
    TxApplication.ActiveSelection.SetItems(txObjectList2);
    TxApplication.ActiveTemporarySelection.SetItems(txObjectList2);
  }

  public static void UnHighlightObject(ITxDisplayableObject displayableObject)
  {
    if (displayableObject == null)
      return;
    TxDisplayableObjectEx.StopEmphasizeBlueObject(displayableObject);
    TxApplication.RefreshDisplay();
  }

  public static void HighlightObject(ITxDisplayableObject displayableObject)
  {
    if (displayableObject == null)
      return;
    TxDisplayableObjectEx.EmphasizeBlueObject(displayableObject);
    TxApplication.RefreshDisplay();
  }

  public static double ConvertStringToDouble(string text)
  {
    return !(text == "") ? Convert.ToDouble(text) : throw new FormatException();
  }

  public static double ComputeLengthStepSize()
  {
    double lengthStepSize;
    switch ((int) TxApplication.Options.Units.LinearUnit)
    {
      case 0:
        lengthStepSize = 20.0;
        break;
      case 1:
        lengthStepSize = 2.0;
        break;
      case 2:
        lengthStepSize = 0.2;
        if (TxApplication.Options.Units.LinearDigitsAfterDecimalPoint == 0)
        {
          lengthStepSize = 1.0;
          break;
        }
        break;
      case 3:
      case 4:
        lengthStepSize = 0.1;
        if (TxApplication.Options.Units.LinearDigitsAfterDecimalPoint == 0)
        {
          lengthStepSize = 1.0;
          break;
        }
        break;
      default:
        lengthStepSize = 1.0;
        break;
    }
    return lengthStepSize;
  }

  public static double ComputeRadiusStepSize()
  {
    TxUnitsOptions.TxLinearUnit linearUnit = TxApplication.Options.Units.LinearUnit;
    double radiusStepSize;
    if (linearUnit != null)
    {
      if (linearUnit - 1 <= 3)
      {
        radiusStepSize = 0.1;
        if (TxApplication.Options.Units.LinearDigitsAfterDecimalPoint == 0)
          radiusStepSize = 1.0;
      }
      else
        radiusStepSize = 1.0;
    }
    else
      radiusStepSize = 1.0;
    return radiusStepSize;
  }

  public static void ComputeCableMountRefData(
    ITxLocatableObject pickedFrame,
    ITxLocatableObject attachmentParent,
    ref TxTransformation relLoc)
  {
    relLoc = TxCableUtilities.GetCableMountRefData(pickedFrame, attachmentParent);
  }

  public static bool ComputeDistanceBetweenFrames(
    ITxLocatableObject frame1,
    ITxLocatableObject frame2,
    out double distance)
  {
    bool distanceBetweenFrames = false;
    distance = 0.0;
    if (frame1 != null && frame2 != null)
    {
      if (frame1 != frame2)
      {
        try
        {
          TxVector txVector1;
          TxVector txVector2;
          frame1.GetMinimalDistance(frame2, ref distance, ref txVector1, ref txVector2);
          distanceBetweenFrames = true;
        }
        catch (Exception ex)
        {
        }
      }
    }
    return distanceBetweenFrames;
  }

  public static ITxObject GetClosestAttachedPreservedCollection(ITxObject obj)
  {
    return TxCableUtilities.GetClosestAttachedPreservedCollection(obj);
  }

  public static bool IsObjectOpenForModeling(ITxObject obj)
  {
    bool flag = false;
    if (obj is ITxComponent)
      flag = (obj as ITxComponent).IsOpenForModeling;
    return flag;
  }

  public static ITxObject GetParentEquipment(ITxObject obj)
  {
    return TxCableUtilities.GetParentEquipment(obj);
  }

  public static bool IsObjectInsideEquipment(ITxObject obj, ITxObject equipment)
  {
    return TxCableUtilities.IsObjectInsideEquipment(obj, equipment);
  }

  public static bool IsObjectAttachedToEquipment(ITxObject obj, ITxObject equipment)
  {
    return TxCableUtilities.IsObjectAttachedToEquipment(obj, equipment);
  }

  public static bool DoAttachmentsContainOnlyZAxisFreeRotatingAttachments(
    List<CApFLEXAttachment> attachments)
  {
    return TxCableUtilities.DoAttachmentsContainOnlyZAxisFreeRotatingAttachments(attachments.Select<CApFLEXAttachment, TxCableAttachment>((Func<CApFLEXAttachment, TxCableAttachment>) (a => a.Attachment)));
  }
}
